﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BadHabit.Factory.Windows {

    /// <summary>
    /// Interaction logic for EditRoles.xaml
    /// </summary>
    public partial class EditRoles : BadHabit.WPFTools.ShapedWindow {

        private bool IsCostEdit { get; set; }

        public EditRoles() {
            InitializeComponent();
            IsCostEdit = false;
        }

        public EditRoles(bool isCostEdit) {
            InitializeComponent();
            IsCostEdit = isCostEdit;
        }

        protected void EditRoles_Loaded(object sender, RoutedEventArgs e) {
            Client.FactoryMainWindow.IsPopUpOpened = true;

            if (IsCostEdit) {                
                typeColumn.Visibility = Visibility.Collapsed;
                backlogItemUnitDataGrid.CanUserAddRows = false;
                backlogItemUnitDataGrid.CanUserDeleteRows = false;
                roleColumn.IsReadOnly = true;
                shortNameColumn.IsReadOnly = true;
            } else {
                hourCostColumn.Visibility = Visibility.Collapsed;
                hourPriceColumn.Visibility = Visibility.Collapsed;
            }

        }


        protected override void OnClosed(EventArgs e) {
            base.OnClosed(e);
            if(Client.FactoryMainWindow!=null)
                Client.FactoryMainWindow.IsPopUpOpened = false;
        }
               
        private void backlogItemUnitDataGrid_InitializingNewItem(object sender, Microsoft.Windows.Controls.InitializingNewItemEventArgs e) {
            Data.ProjectDataSet.BacklogItemUnitRow unitRow = Helper.GetRowFromRowView<Data.ProjectDataSet.BacklogItemUnitRow>(e.NewItem);
            Data.ProjectDataSet dsProject = unitRow.Table.DataSet as Data.ProjectDataSet;

            unitRow.UnitName = FindNewUnitName(0, dsProject);
            unitRow.Required = false;
            unitRow.Type = (short)TeamRoles.TEAM;            
            unitRow.ProjectRow = dsProject.Project[0];
            unitRow.ProjectUId = dsProject.Project[0].ProjectUId;            
        }

        private string FindNewUnitName(int x, Data.ProjectDataSet dsProject) {
            string unitName = "Des " + x;
            if (x == 0)
                unitName = "Des";
            if (dsProject.BacklogItemUnit.FindByUnitNameProjectUId(unitName, dsProject.Project[0].ProjectUId) != null)
                return FindNewUnitName(x + 1, dsProject);
            return unitName;
        }

        private void Save_Executed(object sender, ExecutedRoutedEventArgs e) {
            this.Close();
        }

        private void Undo_Executed(object sender, ExecutedRoutedEventArgs e) {
            Data.ProjectDataSet dsProject = DataContext as Data.ProjectDataSet;
            if (dsProject != null) {
                dsProject.BacklogItemUnit.RejectChanges();
            }
            this.Close();
        }


        

    }
}
